//
//  DDRegisterViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/14.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDRegisterViewController.h"
#import "DDLoginManager.h"

@interface DDRegisterViewController ()<UITextFieldDelegate,UIActionSheetDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollWidth;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (weak, nonatomic) IBOutlet UIView *registerBackView;
@property (weak, nonatomic) IBOutlet UITextField *emailTxf;
@property (weak, nonatomic) IBOutlet UITextField *nameTxf;
@property (weak, nonatomic) IBOutlet UITextField *genderTxf;
@property (weak, nonatomic) IBOutlet UITextField *pwdTxf;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@property (strong, nonatomic) DDLoginManager *manager;
@property (strong, nonatomic) NSString *selectGender;
@end

@implementation DDRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DDLoginManager new];
    [self layoutRegisterView];
}
- (void)layoutRegisterView
{
    self.scrollWidth.constant = ScreenWidth;
    self.scrollHeight.constant = ScreenHeight-64;
    self.registerBtn.layer.cornerRadius = 3.0;
    self.selectGender = @"";
}
#pragma mark --- IBAction
- (IBAction)registerClick:(id)sender
{
    if (![self checkInput]) {
        return;
    }
    
    weak_Self(self);
    [[self.manager registerUser:self.emailTxf.text password:self.pwdTxf.text gender:@"1" name:self.nameTxf.text]subscribeNext:^(id x) {
        [weakSelf showMessageHUD:@"Register success!" delayHide:1.0];
    } error:^(NSError *error) {
        NSString *msg = error.userInfo[@"msg"];
        [weakSelf showMessageHUD:msg delayHide:1.0];
    } completed:^{
        
    }];
}
- (IBAction)genderClick:(id)sender
{
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Male",@"Female",@"Secret", nil];
    [sheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        self.selectGender = @"1";
        self.genderTxf.text = @"Male";
    }else if (buttonIndex == 1){
        self.selectGender = @"2";
        self.genderTxf.text = @"Female";
    }else if (buttonIndex == 2){
        self.selectGender = @"3";
        self.genderTxf.text = @"Secret";
    }
}
- (BOOL)checkInput
{
    if (![NOValidateHelper validateEmail:self.emailTxf.text]) {
        
        return NO;
    }
    if (self.nameTxf.text.length == 0) {
        return NO;
    }
    if (self.selectGender.length == 0) {
        return NO;
    }
    if (self.pwdTxf.text.length == 0) {
        return NO;
    }
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
